package cn.edu.scau.cmi.liweihang.spring.hibernate.dao;
import java.util.List;

import javax.swing.border.StrokeBorder;

import org.hibernate.Transaction;

import cn.edu.scau.cmi.liweihang.spring.hibernate.dao.StudentDAO;
import cn.edu.scau.cmi.liweihang.spring.hibernate.dao.TeacherDAO;
import cn.edu.scau.cmi.liweihang.spring.hibernate.domain.Student;
import cn.edu.scau.cmi.liweihang.spring.hibernate.domain.Teacher;
import cn.edu.scau.cmi.liweihang.spring.hibernate.factory.HibernateSessionFactory;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.stage.Window;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.Background;
import javafx.scene.layout.Border;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.shape.Box;

public class HibernateClient extends Application {
	private Transaction transaction;
	private BorderPane rootBorderPane = new BorderPane();
	private VBox choicePane = new VBox();
	private HBox sqlBox = new HBox();
	private HBox personBox = new HBox();
	private VBox choicesBox = new VBox();
	private HBox inputBox = new HBox();
	private TextArea resulTextArea = new TextArea();

	private ToggleGroup groupSql = new ToggleGroup();
	private RadioButton selectOption = new RadioButton("select");
	private RadioButton insertOption = new RadioButton("insert");
	private RadioButton deleteOption = new RadioButton("delete");
	private RadioButton updateOption = new RadioButton("update");

	private ToggleGroup groupPerson = new ToggleGroup();
	private RadioButton studnetRadio = new RadioButton("student");
	private RadioButton teacherRadio = new RadioButton("teacher");
	private Button button = new Button("commit");
	private Button clearButton = new Button("clear result");
	private Button showStudent = new Button("show studnets");
	private Button showTeacher = new Button("show teachers");
	private Label nameLabel = new Label("name:       ");
	private Label idLabel = new Label("id:            ");
	private Label tutorLabel = new Label("tutor:     ");
	private Button clearInput = new Button("clearInput");
	private TextField nameInput = new TextField();
	private TextField idInput = new TextField();
	private TextField tutorInput = new TextField();

	private StudentDAO studentDAO = new StudentDAO();
	private TeacherDAO teacherDAO = new TeacherDAO();

	public void initSet() {
		this.setFontAndSoOn();
		this.addElement();
		this.addListener();
		this.setFrameSelf();
	}

	@Override
	public void start(Stage primaryStage) {
		try {
			initSet();
			Scene scene = new Scene(rootBorderPane, 800, 600);
			primaryStage.setScene(scene);
			primaryStage.setTitle("hibernate practices");
			primaryStage.show();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 1.设置 字体 颜色 背景 布局等等
	public void setFontAndSoOn() {

		// 初始化单选组selectOption
		selectOption.setSelected(true);
		selectOption.setToggleGroup(groupSql);
		selectOption.setUserData("select");

		updateOption.setToggleGroup(groupSql);
		updateOption.setUserData("update");

		insertOption.setSelected(true);
		insertOption.setToggleGroup(groupSql);
		insertOption.setUserData("insert");

		deleteOption.setToggleGroup(groupSql);
		deleteOption.setUserData("delete");

		studnetRadio.setSelected(true);
		studnetRadio.setToggleGroup(groupPerson);
		studnetRadio.setUserData("student");

		teacherRadio.setToggleGroup(groupPerson);
		teacherRadio.setUserData("teacher");

	};

	// 2.将属性添加到窗体里
	public void addElement() {
		rootBorderPane.setTop(choicePane);
		rootBorderPane.setCenter(resulTextArea);
		sqlBox.getChildren().addAll(insertOption, selectOption, updateOption, deleteOption);
		personBox.getChildren().addAll(studnetRadio, teacherRadio);
		choicesBox.getChildren().addAll(sqlBox, personBox);
		HBox hBox = new HBox();
		hBox.getChildren().addAll(showStudent, showTeacher, clearButton);
		choicePane.getChildren().addAll(choicesBox, inputBox, hBox);
		inputBox.getChildren().addAll(idLabel, idInput, nameLabel, nameInput, tutorLabel, tutorInput, button,
				clearInput);
		// choicePane.setBottom(button);
		// choicePane.setAlignment(choicesBox,Pos.CENTER);
		// choicePane.setAlignment(button, Pos.BASELINE_CENTER);

	};

	// 3.添加事件监听
	public void addListener() {
		studnetRadio.setOnAction(e -> {
			tutorInput.setDisable(false);

		});
		teacherRadio.setOnAction(e -> {
			tutorInput.setDisable(true);
		});
		clearButton.setOnAction(e -> {
			resulTextArea.clear();
		});
		button.setOnAction(e -> {
			String typeSql = (String) groupSql.getSelectedToggle().getUserData();
			String typePerson = (String) groupPerson.getSelectedToggle().getUserData();
			String nameString = nameInput.getText();
			String idString = idInput.getText();
			String tutorString = tutorInput.getText();
			if (nameString.length() < 1) {
				nameString = null;
			}
			if (idString.length() < 1) {
				idString = null;
			}
			if (tutorString.length() < 1) {
				tutorString = null;
			}
			if ("student".equals(typePerson)) {
				if ("insert".equals(typeSql)) {
					if (studentDAO.findById(Long.parseLong(idString)) != null) {
						resulTextArea.appendText("student id= " + idString + "already exist!\n");
						return;
					}
					Teacher teacher = null;
					if (tutorString != null) {
						teacher = teacherDAO.findById(Long.parseLong(tutorString));
						if (teacher == null) {
							resulTextArea.appendText("teacher id= " + tutorString + " does't exist!\n");
							return;
						}
					}
					Student student = new Student(Long.parseLong(idString), teacher, nameString);
					studentDAO.save(student);
					resulTextArea.appendText("student id:" + student.getId() + "-student name:" + student.getName()
					+ "-student tutor:" + student.getTeacher() +"  insert success!\n");
				} else if ("select".equals(typeSql)) {
					Student student = studentDAO.findById(Long.parseLong(idString));
					if (student != null) {
						resulTextArea.appendText("student id:" + student.getId() + "-student name:" + student.getName()
								+ "-student tutor:" + student.getTeacher());
					} else {
						resulTextArea.appendText("不存在\n");
					}
				} else if ("update".equals(typeSql)) {
					Student student = studentDAO.findById(Long.parseLong(idString));
					if (student == null) {
						resulTextArea.appendText("student id= " + idString + " doesn't exit");
						return;
					}
					student.setName(nameString);
					student.setTeacher(teacherDAO.findById(Long.parseLong(tutorString)));
					studentDAO.update(student);
					resulTextArea.appendText("update success\n");
				} else {
					Student student = studentDAO.findById(Long.parseLong(idString));
					if (student == null) {
						resulTextArea.appendText("student id= " + idString + " doesn't exit");
						return;
					}
					transaction = HibernateSessionFactory.getSession().beginTransaction();
					studentDAO.delete(student);
					transaction.commit();
					resulTextArea
							.appendText("student id= " + idString + " name = " + nameString + " delete success!\n");
				}
			} else {
				Teacher teacher = teacherDAO.findById(Long.parseLong(idString));
				if ("insert".equals(typeSql)) {
					if (teacher == null) {
						teacher = new Teacher();
						teacher.setId(Long.parseLong(idString));
						teacher.setName(nameString);
						transaction = HibernateSessionFactory.getSession().beginTransaction();
						teacherDAO.save(teacher);
						transaction.commit();
						resulTextArea.appendText(
								"insert teacher id = " + idString + " name = " + nameString + " success!\n");

					} else {
						resulTextArea.appendText(
								"insert teacher id = " + idString + "fail id = " + idString + "already exit!\n");
					}
				} else if ("select".equals(typeSql)) {
					if (teacher == null) {
						resulTextArea.appendText(
								"select teacher id = " + idString + " name = " + nameString + " doesn't exit!\n");
					} else {
						resulTextArea.appendText(
								"teacher id =" + teacher.getId() + "teacher name=" + teacher.getName() + "\n");
					}

				} else if ("update".equals(typeSql)) {
					if (teacher == null) {
						resulTextArea.appendText("teacher id=" + idString + "doesn't exit\n");
					} else {
						teacher.setName(nameString);
						teacherDAO.update(teacher);
						resulTextArea
								.appendText("teacher id=" + idString + "name = " + nameString + "update success\n");
					}
				} else if ("delete".equals(typeSql)) {
					if (teacher == null) {
						resulTextArea.appendText("teacher id=" + idString + "doesn't exist!\n");
					} else {
						transaction = HibernateSessionFactory.getSession().beginTransaction();
						teacherDAO.delete(teacher);
						transaction.commit();
						resulTextArea
								.appendText("teacher id=" + idString + "name = " + nameString + "delete success\n");
					}
				}
			}
		});
		clearInput.setOnAction(e -> {
			idInput.clear();
			nameInput.clear();
			tutorInput.clear();
		});
		showStudent.setOnAction(e -> {
			List<Student> students = studentDAO.findAll();
			resulTextArea.appendText("----------------------------------------------------------\n");
			resulTextArea.appendText("show students\n");
			for (Student student : students) {
				resulTextArea.appendText("student: id = " + student.getId() + " name = " + student.getName()
						+ " tutor = " + student.getTeacher() + "\n");
			}
			resulTextArea.appendText("----------------------------------------------------------\n");
		});
		showTeacher.setOnAction(e -> {
			List<Teacher> teachers = (List<Teacher>) teacherDAO.findAll();
			resulTextArea.appendText("----------------------------------------------------------\n");
			resulTextArea.appendText("show teachers\n");
			for (Teacher teacher : teachers) {
				resulTextArea.appendText("teacher: id = " + teacher.getId() + " name = " + teacher.getName() + "\n");
			}
			resulTextArea.appendText("----------------------------------------------------------\n");
		});
	}

	// 4.设置窗体自身
	public void setFrameSelf() {
	};

	public static void main(String[] args) {
		launch(args);
	}
}
